package com.vessel.decoration.mvp.ui.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.vessel.decoration.BuildConfig;
import com.vessel.decoration.R;
import com.vessel.decoration.app.data.entity.ProductListResponse;
import com.vessel.decoration.mvp.ui.listener.OnProductClickListener;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

public class ProductManagerAdapter extends DefaultAdapter<ProductListResponse.ProductsBean> {

    private OnProductClickListener onProductClickListener;

    public ProductManagerAdapter(List<ProductListResponse.ProductsBean> infos) {
        super(infos);
    }

    public void setOnProductClickListener(OnProductClickListener onProductClickListener) {
        this.onProductClickListener = onProductClickListener;
    }

    @Override
    public BaseHolder<ProductListResponse.ProductsBean> getHolder(View v, int viewType) {
        return new ProductHolder(v);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_me_product;
    }

    class ProductHolder extends BaseHolder<ProductListResponse.ProductsBean> {

        @BindView(R.id.product_item_pic) ImageView mPic;
        @BindView(R.id.product_item_name) TextView mName;
        @BindView(R.id.product_item_remark) TextView mRemark;
        @BindView(R.id.product_item_price) TextView mPrice;
        @BindView(R.id.product_item_manage) TextView mManage;

        private AppComponent mAppComponent;

        public ProductHolder(View itemView) {
            super(itemView);
            if (mAppComponent == null) {
                mAppComponent = ArmsUtils.obtainAppComponentFromContext(itemView.getContext());
            }
        }

        @Override
        public void setData(ProductListResponse.ProductsBean data, int position) {
            Observable.just(data).subscribe(productsBean -> {
                mName.setText(productsBean.getProductName());
                mRemark.setText(productsBean.getRemark());
                mPrice.setText("" + productsBean.getPrice());
                mManage.setOnClickListener(view -> {
                    if (onProductClickListener != null) {
                        onProductClickListener.onItemClick(view, data);
                    }
                });
                if (mAppComponent != null) {
                    mAppComponent.imageLoader()
                            .loadImage(itemView.getContext(), ImageConfigImpl
                                    .builder()
                                    .imageView(mPic)
                                    .url(BuildConfig.APP_DOMAIN + data.getProductPic())
                                    .build());
                }
            });
        }
    }
}
